﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T2CalcLib.Repositories
{
    public class EvEDataRepository
    {
        private String sqLiteConnectionString { get; set; }

        public EvEDataRepository()
        {
            // TODO: Wenn geleert dann in die Lib übernehmen oder so damits deployt wird
            this.sqLiteConnectionString = @"Data Source=evedump.db;";
        }

        public Invention GetInventionByT2TypeID(Int64 typeID, Decimal invChance)
        {
            Invention invention = new Invention();
            invention.Datacores = new List<Material>();
            invention.InventionChance = invChance;

            using (SQLiteConnection connection = new SQLiteConnection(this.sqLiteConnectionString))
            {
                connection.Open();

                using (SQLiteCommand cmd = new SQLiteCommand(SQL.Querys.GetInventionItems, connection))
                {
                    SQLiteParameter itemId = new SQLiteParameter("t2typeID", typeID);
                    cmd.Parameters.Add(itemId);
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Material mat = new Material();
                            mat.TypeID = reader.GetInt64(0);
                            mat.TypeName = reader.GetString(1);
                            mat.Amount = reader.GetInt32(2);

                            invention.Datacores.Add(mat);
                        }
                    }
                }

                connection.Close();
            }

            return invention;
        }

        public Production GetProducitonByT2TypeID(Int64 typeID, Int32 me, Int32 runPerBPC)
        {
            Production production = new Production();
            production.MaterialsAndAmount = new Dictionary<Material, Decimal>();
            production.MaterialExtras = new List<Material>();
            production.MaterialEfficiency = me;
            production.TypeID = typeID;
            production.RunPerBPC = 10;

            using (SQLiteConnection connection = new SQLiteConnection(this.sqLiteConnectionString))
            {
                connection.Open();

                using (SQLiteCommand cmd = new SQLiteCommand(SQL.Querys.GetT2ItemData, connection))
                {
                    SQLiteParameter itemId = new SQLiteParameter("t2typeID", typeID);
                    cmd.Parameters.Add(itemId);
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            production.TypeName = reader.GetString(1);
                            production.AmountPerRun = reader.GetInt32(2);
                            production.BaseWaste = reader.GetInt32(3);
                        }
                    }
                }

                using (SQLiteCommand cmd = new SQLiteCommand(SQL.Querys.GetProductionItems, connection))
                {
                    SQLiteParameter itemId = new SQLiteParameter("t2typeID", typeID);
                    cmd.Parameters.Add(itemId);
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Material material = new Material();
                            material.TypeID = reader.GetInt64(0);
                            material.TypeName = reader.GetString(1);
                            material.Amount = reader.GetInt32(2);

                            production.MaterialsAndAmount.Add(material, material.Amount);
                        }
                    }
                }

                using (SQLiteCommand cmd = new SQLiteCommand(SQL.Querys.GetProductionExtras, connection))
                {
                    SQLiteParameter itemId = new SQLiteParameter("t2typeID", typeID);
                    cmd.Parameters.Add(itemId);
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Material material = new Material();
                            material.TypeID = reader.GetInt64(0);
                            material.TypeName = reader.GetString(1);
                            material.Amount = reader.GetDecimal(2) * (decimal)reader.GetInt32(3);

                            production.MaterialExtras.Add(material);
                        }
                    }
                }

                connection.Close();
            }

            production.RecalculateMatrialAmount();

            return production;
        }

        public List<Tuple<Int64, String>> GetT2Items()
        {
            List<Tuple<Int64, String>> data = new List<Tuple<Int64, String>>();

            using (SQLiteConnection connection = new SQLiteConnection(this.sqLiteConnectionString))
            {
                connection.Open();

                using (SQLiteCommand cmd = new SQLiteCommand(SQL.Querys.GetT2Items, connection))
                {
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            data.Add(new Tuple<Int64, String>(reader.GetInt64(0), reader.GetString(1)));
                        }
                    }
                }

                connection.Close();
            }

            return data;
        }
    }
}
